package com.iot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iot.core.JsonHelper;
import com.iot.core.LResult;
import com.iot.core.PageData;
import com.iot.pojo.MTrole;
import com.iot.pojo.MTrole_authority;
import com.iot.service.TroleService;
import com.iot.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/role/")
public class TRoleController extends BaseController {

    @Autowired
    private TroleService service;

    @ResponseBody
    @RequestMapping("save")
    public String save(@RequestParam String bean) {
        try {
            //  String menu = request.getParameter("menu");

            MTrole entity = JsonHelper.toBean(bean, MTrole.class);
            if (entity.getROLE_ID() > 0) {
                service.update(entity);
            } else {
                service.insert(entity);
            }
            return LResult.Success(entity.getROLE_ID()).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("get")
    public String getByid(@RequestParam Integer id) {
        try {
            //  String menu = request.getParameter("menu");
            MTrole bean = service.single(id);
            return LResult.Success(bean).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("del")
    public String deleteByid(@RequestParam Integer id) {
        try {
            //  String menu = request.getParameter("menu");
            service.deleteRole(id);
            return LResult.Success().toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("query")
    public String queryByPage() {

        try {
            //" AND SNAME LIKE '" + key + "%'"
            //   Map map = request.getParameterMap(); // 获取所有参数
            PageData map = getPageData();
            if (!map.containsKey("appId")) {
                map.put("appId", 0);
            }
            if (!map.containsKey("sname")) {
                map.put("sname", "");
            }
            PageHelper.startPage(Common.toInt(map.get("page")), Common.toInt(map.get("limit")));
            List<Map<String, Object>> list = service.fetchByPage(map);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
            return LResult.Success(pageInfo.getTotal(), pageInfo.getList()).toJson();

        } catch (Exception ex) {
            return  LResult.Error("数据错误").toJson();
        }
    }

    //    public String queryData( @RequestParam("page") Integer  page,@RequestParam("limit") int  limit) {
    @ResponseBody
    @RequestMapping("auth")
    public String getAuthorityByRoleIdList(@RequestParam Integer id) {

        try {

            List<Map<String, String>> list = service.getAuthorityByRoleIdList(id);

            return LResult.Success(list).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("saveAuth")
    public String saveAuthority(@RequestParam String list) {

        try {
            List<MTrole_authority> data = JsonHelper.toList(list, MTrole_authority.class);
            service.saveAuthority(data);
            return LResult.Success("").toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }


}
